<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of demgmm5</title>
  <meta name="keywords" content="demgmm5">
  <meta name="description" content="DEMGMM5 Demonstrate density modelling with a PPCA mixture model.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; demgmm5.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>demgmm5
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>DEMGMM5 Demonstrate density modelling with a PPCA mixture model.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This is a script file. </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">DEMGMM5 Demonstrate density modelling with a PPCA mixture model.

    Description
     The problem consists of modelling data generated by a mixture of
    three Gaussians in 2 dimensions with a mixture model using full
    covariance matrices.  The priors are 0.3, 0.5 and 0.2; the centres
    are (2, 3.5), (0, 0) and (0,2); the variances are (0.16, 0.64) axis
    aligned, (0.25, 1) rotated by 30 degrees and the identity matrix. The
    first figure contains a scatter plot of the data.

    A mixture model with three one-dimensional PPCA components is trained
    using EM.  The parameter vector is printed before training and after
    training.  The parameter vector consists of priors (the column), and
    centres (given as (x, y) pairs as the next two columns).

    The second figure is a 3 dimensional view of the density function,
    while the third shows the axes of the 1-standard deviation ellipses
    for the three components of the mixture model together with the one
    standard deviation along the principal component of each mixture
    model component.

    See also
    <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">GMM</a>, <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">GMMINIT</a>, <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">GMMEM</a>, <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">GMMPROB</a>, <a href="ppca.html" class="code" title="function [var, U, lambda] = ppca(x, ppca_dim)">PPCA</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>	GMM	Creates a Gaussian mixture model with specified architecture.</li><li><a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>	GMMEM	EM algorithm for Gaussian mixture model.</li><li><a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>	GMMINIT Initialises Gaussian mixture model from data</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <span class="comment">%DEMGMM5 Demonstrate density modelling with a PPCA mixture model.</span>
0002 <span class="comment">%</span>
0003 <span class="comment">%    Description</span>
0004 <span class="comment">%     The problem consists of modelling data generated by a mixture of</span>
0005 <span class="comment">%    three Gaussians in 2 dimensions with a mixture model using full</span>
0006 <span class="comment">%    covariance matrices.  The priors are 0.3, 0.5 and 0.2; the centres</span>
0007 <span class="comment">%    are (2, 3.5), (0, 0) and (0,2); the variances are (0.16, 0.64) axis</span>
0008 <span class="comment">%    aligned, (0.25, 1) rotated by 30 degrees and the identity matrix. The</span>
0009 <span class="comment">%    first figure contains a scatter plot of the data.</span>
0010 <span class="comment">%</span>
0011 <span class="comment">%    A mixture model with three one-dimensional PPCA components is trained</span>
0012 <span class="comment">%    using EM.  The parameter vector is printed before training and after</span>
0013 <span class="comment">%    training.  The parameter vector consists of priors (the column), and</span>
0014 <span class="comment">%    centres (given as (x, y) pairs as the next two columns).</span>
0015 <span class="comment">%</span>
0016 <span class="comment">%    The second figure is a 3 dimensional view of the density function,</span>
0017 <span class="comment">%    while the third shows the axes of the 1-standard deviation ellipses</span>
0018 <span class="comment">%    for the three components of the mixture model together with the one</span>
0019 <span class="comment">%    standard deviation along the principal component of each mixture</span>
0020 <span class="comment">%    model component.</span>
0021 <span class="comment">%</span>
0022 <span class="comment">%    See also</span>
0023 <span class="comment">%    GMM, GMMINIT, GMMEM, GMMPROB, PPCA</span>
0024 <span class="comment">%</span>
0025 
0026 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0027 
0028 
0029 ndata = 500;
0030 data = randn(ndata, 2);
0031 prior = [0.3 0.5 0.2];
0032 <span class="comment">% Mixture model swaps clusters 1 and 3</span>
0033 datap = [0.2 0.5 0.3];
0034 datac = [0 2; 0 0; 2 3.5];
0035 datacov = repmat(eye(2), [1 1 3]);
0036 data1 = data(1:prior(1)*ndata,:);
0037 data2 = data(prior(1)*ndata+1:(prior(2)+prior(1))*ndata, :);
0038 data3 = data((prior(1)+prior(2))*ndata +1:ndata, :);
0039 
0040 <span class="comment">% First cluster has axis aligned variance and centre (2, 3.5)</span>
0041 data1(:, 1) = data1(:, 1)*0.1 + 2.0;
0042 data1(:, 2) = data1(:, 2)*0.8 + 3.5;
0043 datacov(:, :, 3) = [0.1*0.1 0; 0 0.8*0.8];
0044 
0045 <span class="comment">% Second cluster has variance axes rotated by 30 degrees and centre (0, 0)</span>
0046 rotn = [cos(pi/6) -sin(pi/6); sin(pi/6) cos(pi/6)];
0047 data2(:,1) = data2(:, 1)*0.2;
0048 data2 = data2*rotn;
0049 datacov(:, :, 2) = rotn' * [0.04 0; 0 1] * rotn;
0050 
0051 <span class="comment">% Third cluster is at (0,2)</span>
0052 
0053 data3(:, 2) = data3(:, 2)*0.1;
0054 data3 = data3 + repmat([0 2], prior(3)*ndata, 1);
0055 
0056 <span class="comment">% Put the dataset together again</span>
0057 data = [data1; data2; data3];
0058 
0059 ndata = 100;            <span class="comment">% Number of data points.</span>
0060 noise = 0.2;            <span class="comment">% Standard deviation of noise distribution.</span>
0061 x = [0:1/(2*(ndata - 1)):0.5]';
0062 randn(<span class="string">'state'</span>, 1);
0063 rand(<span class="string">'state'</span>, 1);
0064 t = sin(2*pi*x) + noise*randn(ndata, 1);
0065 
0066 <span class="comment">% Fit three one-dimensional PPCA models</span>
0067 ncentres = 3;
0068 ppca_dim = 1;
0069 
0070 clc
0071 disp(<span class="string">'This demonstration illustrates the use of a Gaussian mixture model'</span>)
0072 disp(<span class="string">'with a probabilistic PCA covariance structure to approximate the'</span>)
0073 disp(<span class="string">'unconditional probability density of data in a two-dimensional space.'</span>)
0074 disp(<span class="string">'We begin by generating the data from a mixture of three Gaussians and'</span>)
0075 disp(<span class="string">'plotting it.'</span>)
0076 disp(<span class="string">' '</span>)
0077 disp(<span class="string">'The first cluster has axis aligned variance and centre (0, 2).'</span>)
0078 disp(<span class="string">'The variance parallel to the x-axis is significantly greater'</span>)
0079 disp(<span class="string">'than that parallel to the y-axis.'</span>)
0080 disp(<span class="string">'The second cluster has variance axes rotated by 30 degrees'</span>)
0081 disp(<span class="string">'and centre (0, 0).  The third cluster has significant variance'</span>)
0082 disp(<span class="string">'parallel to the y-axis and centre (2, 3.5).'</span>)
0083 disp(<span class="string">' '</span>)
0084 disp(<span class="string">'Press any key to continue.'</span>)
0085 pause
0086 
0087 fh1 = figure;
0088 plot(data(:, 1), data(:, 2), <span class="string">'o'</span>)
0089 set(gca, <span class="string">'Box'</span>, <span class="string">'on'</span>)
0090 axis equal
0091 hold on
0092 
0093 mix = <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>(2, ncentres, <span class="string">'ppca'</span>, ppca_dim);
0094 options = foptions;
0095 options(14) = 10;
0096 options(1) = -1;  <span class="comment">% Switch off all warnings</span>
0097 
0098 <span class="comment">% Just use 10 iterations of k-means in initialisation</span>
0099 <span class="comment">% Initialise the model parameters from the data</span>
0100 mix = <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>(mix, data, options);
0101 disp(<span class="string">'The mixture model has three components with 1-dimensional'</span>)
0102 disp(<span class="string">'PPCA subspaces.  The model parameters after initialisation using'</span>)
0103 disp(<span class="string">'the k-means algorithm are as follows'</span>)
0104 disp(<span class="string">'    Priors        Centres'</span>)
0105 disp([mix.priors' mix.centres])
0106 disp(<span class="string">' '</span>)
0107 disp(<span class="string">'Press any key to continue'</span>)
0108 pause
0109 
0110 options(1)  = 1;        <span class="comment">% Prints out error values.</span>
0111 options(14) = 30;        <span class="comment">% Number of iterations.</span>
0112 
0113 disp(<span class="string">'We now train the model using the EM algorithm for up to 30 iterations.'</span>)
0114 disp(<span class="string">' '</span>)
0115 disp(<span class="string">'Press any key to continue.'</span>)
0116 pause
0117 
0118 [mix, options, errlog] = <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>(mix, data, options);
0119 disp(<span class="string">'The trained model has priors and centres:'</span>)
0120 disp(<span class="string">'    Priors        Centres'</span>)
0121 disp([mix.priors' mix.centres])
0122 
0123 <span class="comment">% Now plot the result</span>
0124 <span class="keyword">for</span> i = 1:ncentres
0125   <span class="comment">% Plot the PC vectors</span>
0126   v = mix.U(:,:,i);
0127   start=mix.centres(i,:)-sqrt(mix.lambda(i))*(v');
0128   endpt=mix.centres(i,:)+sqrt(mix.lambda(i))*(v');
0129   linex = [start(1) endpt(1)];
0130   liney = [start(2) endpt(2)];
0131   line(linex, liney, <span class="string">'Color'</span>, <span class="string">'k'</span>, <span class="string">'LineWidth'</span>, 3)
0132 
0133   <span class="comment">% Plot ellipses of one standard deviation</span>
0134   theta = 0:0.02:2*pi;
0135   x = sqrt(mix.lambda(i))*cos(theta);
0136   y = sqrt(mix.covars(i))*sin(theta);
0137   <span class="comment">% Rotate ellipse axes</span>
0138   rot_matrix = [v(1) -v(2); v(2) v(1)];
0139   ellipse = (rot_matrix*([x; y]))';
0140   <span class="comment">% Adjust centre</span>
0141   ellipse = ellipse + ones(length(theta), 1)*mix.centres(i,:);
0142   plot(ellipse(:,1), ellipse(:,2), <span class="string">'r-'</span>)
0143 <span class="keyword">end</span>
0144 
0145 disp(<span class="string">' '</span>)
0146 disp(<span class="string">'Press any key to exit'</span>)
0147 pause
0148 close (fh1);
0149 clear all;</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>